/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsos.samcomm.userManagement.boundary.jsf;

import de.hsos.samcomm.boundary.Community;
import de.hsos.samcomm.userManagement.entity.Member;
import de.hsos.samcomm.common.MessageHandler;
import java.io.Serializable;
import java.util.MissingResourceException;
import javax.ejb.EJB;
import javax.enterprise.inject.Model;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.Size;

/**
 *
 * @author felix
 */
@ManagedBean(name="registerBean")
@SessionScoped
public class RegisterBean implements Serializable {
    @Inject
    Community community;

    @Size(min=3, max=30, message="{register.nickname.size}")
    private String nickname;
    private String email;
    @Size(min=6, max=20, message="{register.password.size}")
    private String pwd1;
    private String pwd2;
    private Member newMember;

    /**
     * Default Konstruktor.
     */
    public RegisterBean() {}

    /**
     * Registriert einen neuen Benutzer.
     *
     * @return die Adresse der neuen Seite.
     */
    public String newMember() {
        try {
            if (pwd1.equals(pwd2)) {
                newMember = community.createMember(nickname, email, pwd1);
                //MessageHandler.addContextFacesMessage(FacesMessage.SEVERITY_INFO, "register.message.success", "");
            }

            return (newMember != null) ? "./login.xhtml" : "";
        }
        catch (MissingResourceException ex) {
            MessageHandler.addExceptionFacesMessage(ex);
        }

        return "";
    }

    /**
     * Geht auf die Uebersichtsseite.
     *
     * @return
     */
    public String gotoHome() {
        clear();
        return "HOME";
    }

    /**
     * Setzt alle Strings zurueck.
     */
    private void clear() {
        nickname = "";
        email = "";
        pwd1 = "";
        pwd2 = "";
    }

    /**
     * Get-Methode fuer den Benutzernamen.
     *
     * @return nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Get-Methode fuer die E-Mail.
     *
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Get-Methode fuer das erste Passwort.
     *
     * @return pwd1
     */
    public String getPwd1() {
        return pwd1;
    }

    /**
     * Get-Methode fuer das zweite Passwort.
     *
     * @return pwd2
     */
    public String getPwd2() {
        return pwd2;
    }

    /**
     * Get-Methode fuer den neuen Benutzer.
     *
     * @return newMember
     */
    public Member getNewMember() {
        return newMember;
    }

    /**
     * Set-Methode fuer den Benutzernamen.
     *
     * @param nickname
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * Set-Methode fuer die E-Mail.
     *
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Set-Methode fuer das erste Passwort.
     *
     * @param pwd1
     */
    public void setPwd1(String pwd1) {
        this.pwd1 = pwd1;
    }

    /**
     * Set-Methode fuer das zweite Passwort.
     *
     * @param pwd2
     */
    public void setPwd2(String pwd2) {
        this.pwd2 = pwd2;
    }

    /**
     * Set-Methode fuer den neuen Benutzer.
     * 
     * @param newMember
     */
    public void setNewMember(Member newMember) {
        this.newMember = newMember;
    }
}
